<?php
require('../includes/init.php');
$domain = "https://www.pentalink.com";
require('../member/session.php');
require('../includes/lib_order.php');
require('../includes/cls_crumb.php');
require(TMP_PATH . '/config/cfg_pay_type.php');


if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['act'] == 'finishPay')
{
    require(ROOT_PATH . '/includes/cls_image.php');
    $img = new cls_image();
    $img->images_dir = '';
    $imgsrc = "/upload" . $img->upload_image($_FILES['file']);

	$userId = $_SESSION[SESSION_PREFIX]['user_id'];
    $sql = "update order_info set pay_status = 1, pay_voucher = '$imgsrc' where user_id = $userId and order_id = " . $_POST['order_id'];
	$db->query($sql);

    header("Location: ./order.php?id=".$_POST["id"]);
    return;
}

$order_sn = isset($_GET['id'])?$_GET['id']:'';
if($order_sn=='')
{
	header("Location: ./index.php");
    return;
}
$order_info = order_info(0, $order_sn);

if($order_info['user_id']<>$_SESSION[SESSION_PREFIX]['user_id'])
{
	header("Location: ./index.php");
    return;
}

if($_SERVER['REQUEST_METHOD'] == 'GET' && $_GET['act'] == 'cancleOrder')
{
	$userId = $_SESSION[SESSION_PREFIX]['user_id'];
    $sql = "update order_info set order_status = -2 where user_id = $userId and order_id = " . $_GET['order_id'];
	$db->query($sql);

    $orderDetails = $db->getAll("select * from order_detail where order_id = ". $_GET['order_id']);
    foreach($orderDetails as $orderDetail){
        $sql = "UPDATE `product_detail` SET `stock`=`stock`+".intval($orderDetail['pcount'])." WHERE `pd_id`='".$orderDetail['pd_id']."';";
        $db->query($sql);
    }

	$rs_arr = array(
        "type" => 1
    );
    die(json_encode($rs_arr));
}

?>
<?php include('./header.php');?>
<style>
    .header_div{
        z-index: 10;
    }
    .wj_sitenav{
        z-index: 999!important;
    }
    .cut_item_price p{
        margin: 0!important;
    }
    
</style>
<?php
$sql = "SELECT count(*) FROM `cart` WHERE `session_id`='".$_SESSION[SESSION_PREFIX]['session_id']."';";
$cart_sum = $db->getOne($sql);
?>
            <link rel="stylesheet" href="../css/new/bootstrap.min.css">
            <!-- 头部搜索框以及导航栏 -->
            <div class="header_div">
                <?php include('./common/search.php');?>
                <?php include('./common/nav.php');?>
            </div>
            <!-- 标签 -->
            <div class="post_position">
                <span class="home_span"><a href="./index.php">首页 > </a></span>
                <span><a>订单</a></span>
            </div>

            <div class="order_content box">
                <h2 class="tit_h2 box" style="margin-top: 0">收货信息：</h2>
                <div class="select_input box">
                    <h3 class="tit_h3 box">
                        <?php
                        $province = $db->getOne("SELECT `region_name` FROM `region` WHERE `region_id`='".$order_info['province']."';");
                        $city = $db->getOne("SELECT `region_name` FROM `region` WHERE `region_id`='".$order_info['city']."';");
                        $district = $db->getOne("SELECT `region_name` FROM `region` WHERE `region_id`='".$order_info['district']."';");
                        ?>
                    收货人：<?php echo $order_info['consignee'] . "，"
                            . $province . "，"
                            . $city . "，"
                            . $district . "，"
                            . $order_info['address'] . "，"
                            . $order_info['mobile'] . "，"
                            . $order_info['tel'];
                     ?>
                    </h3>
                </div>

                <!-- 可支付订单 -->
                <div class="cat_pay_order box">
                    <?php if($order_info["order_status"] == 0 && $order_info["pay_status"] == 0): ?>
                    <h1 class="box">可支付订单</h1>
                    <?php elseif($order_info["order_status"] == -2): ?>
                        <h1 class="box">已取消订单</h1>
                    <?php elseif($order_info["order_status"] == 1 && $order_info["pay_status"] == 1): ?>
                        <h1 class="box">已支付订单</h1>
                    <?php elseif($order_info["order_status"] == 0 && $order_info["pay_status"] == 1): ?>
                        <h1 class="box">已支付待审核订单</h1>
                    <?php endif; ?>
                    <?php if($order_info["order_status"] != -2): ?>
                    <p class="cat_pay_order_tip">为了保证及时处理您的订单，请您下单后及时付款，并联系我们客服。</p>
                    <?php endif; ?>

                    <div class="cat_pay_order_info box">
                        <div class="fl">
                            <p class="p_one">订单号：<span class="e9"><?php echo $order_info['order_sn']; ?></span></p>
                            <p class="p_two">支付金额：<span class="e9 bold">￥<?php echo $order_info['total_fee']; ?></span></p>
                        </div>
                        <!-- <div class="fl buy_unit">购买单位：<span class="bold">全购企业</span></div> -->
                        <!-- <div class="fr countdown">剩余时间：15：00：00</div> -->
                    </div>
                </div>

                <table class="table table-condensed">
                    <thead>
                        <tr>
                            <th>商品型号</th>
                            <th>规格</th>
                            <th>包装</th>
                            <th>交货类型</th>
                            <th>单价</th>
                            <th>数量</th>
                            <th>小计</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $sql = "SELECT * FROM `order_detail` WHERE `order_id`='".$order_info['order_id']."';";
                    $order_goods = $db->getAll($sql);
                    if(!empty($order_goods)):
                    foreach($order_goods as $key => $goods):
                    $sql = "SELECT * FROM `product` WHERE `product_id`='".$goods['product_id']."'";
                    $product = $db->getRow($sql);

                    $sql = "SELECT *,if(`sort_order` > '0',1,0) AS `is_order` FROM `product_pic` WHERE `ftype`=1 AND `product_id`='".$product['product_id']."' ORDER BY `is_order` DESC, `sort_order` LIMIT 1;";
                    $pic = $db->getRow($sql);

                    $decimals = $product['bigcate_id']=='1'?4:2;

                    //规格信息
                    $sql = "SELECT * FROM `product_detail` WHERE `pd_id`='".$goods['pd_id']."';";
                    $gg = $db->getRow($sql);
                    $cate_arr = explode(',', $product['cate_ids']);
                    $cateId = $cate_arr[0];
                    ?>
                        <tr>
                            <td>
                                <a href="./proDetail.php?id=<?php echo $product['product_id'];?>&cate_id=<?php echo $cateId;?>">
                                    <div class="td_img"><img src="<?php echo $domain; ?>/upload<?php echo $pic['pic100'];?>" alt="<?php echo $product['title'];?>"></div>
                                </a>
                                <div class="td_p">
                                    <a href="./proDetail.php?id=<?php echo $product['product_id'];?>&cate_id=<?php echo $cateId;?>">
                                        <p class="p1"><?php echo $product['title'];?></p>
                                    </a>
                                <p>
                                    <?php echo $gg['sku'];?><br />
                                    <?php if($gg['brand_id']){?>品牌：<?php echo $cfg_brand[$gg['brand_id']];?><?php }?>
                                    <?php if($gg['is_stocklast']=='1'):?>
                                    <br />库存：<?php echo $gg['stock'];?>
                                    <br /><span class="all-kinds-mark clearance-end">售完即止</span>
                                    <?php endif;?>
                                    </div>
                                    <?php if($gg['is_stocklast']=='1'):?>
                                    <b class="goods-mark goods-mark-clearance goods-mark-imglist">清仓</b>
                                    <?php endif;?>
                                </p>
                            </div>
                            </td>
                            <td><?php echo $gg['xy_value'];?></td>
                            <td><?php echo $gg['pkg'];?>个/包</td>
                            <td class="e9"> <?php if($goods['stock_status'] == 1){?> 现货 <?php }else{ ?> 部分现货 <?php } ?></td>

                            <!-- <td>
                                <p>预计：2023-06-01</p>
                                <p>采购交期：<span  class="e9">43天</span></p>
                            </td> -->
                            <td><?php echo $goods['price'];?></td>

                            <td><?php echo $goods['pcount'];?></td>
                            <td class="e9 bold">￥ <?php echo $goods['price'] * $goods['pcount'];?></td> 
                        </tr>
                    <?php
                    endforeach;
                    endif;
                    ?>
                    </tbody>
                </table>
 
                <div class="order_payment_detail box">
                    <ul>
                        <li><p>折扣：<span class="e9">-￥<?php echo $order_info["discount"];?></span></p></li>
                        <li><p>运费：<span  class="e9">+￥<?php echo $order_info["shipping_fee"];?></span></p></li>
                        <li><p>商品总计：<span  class="e9">￥<?php echo $order_info["product_amount"];?></span></p></li>
                        <li><p>订单总额：<span  class="e9">￥<?php echo $order_info["total_fee"]; ?></span></p></li>
                    </ul>
                </div>

                <div class="order_payment_btn box">
                    <?php if($order_info["order_status"] == 0 && $order_info["pay_status"] == 0): ?>
                    <div class="order_commit_btns">
                        <a class="remove_btn" onclick="cancleOrder(<?php echo $order_info['order_id']?>)">取消订单</a>
                    </div>
                    <?php endif;?>

                    <?php if($order_info["order_status"] == 0 && $order_info["pay_status"] == 0): ?>
                    <div class="order_payment_way box">
                        <h1>选择支付方式</h1>
                        <div class="payment_way_content box">
                            <div class="payment_way_radio">
                                <div class="payment_way fl box" onclick="xianxiaPay()">
                                    <input id="xianxia_pay" class="radio" type="radio" name="pay_type" checked="true" />
                                    <span>线下支付</span>
                                </div>

                                <div class="payment_way fr box" style="position: relative;">
                                    <input id="zfb_pay" class="radio" type="radio" name="pay_type" />
                                    <span>支付宝</span>
                                    <form name=alipayment action=/api/pay/alipay/pagepay/pagepay.php method="post" target="_blank">
                                    <div id="ocalipay" class="ocalipay"> <strong>
                                    <label>为了保证及时处理您的订单，请您及时付款！</label>
                                    </strong>
                                    <input style="position: absolute;width: 100%;left: 0;top: 0;background: white;border: 1px solid red;height: 100%;opacity: 0;"
                                    type="submit" name="btnAlipay" value="" id="btnAlipay" onclick="zhifubaoPay()" />
                                    <!--<input type="hidden" name="paymode" value="<?php echo $order_info['paymode'];?>" />-->
                                    <input type="hidden" name="WIDout_trade_no" id="WIDout_trade_no" value="<?php echo $order_info['order_sn'];?>" />
                                    <input type="hidden" name="WIDsubject" id="WIDsubject" value="<?php echo $order_info['order_sn'];?>" />
                                    <input type="hidden" name="WIDtotal_amount" id="WIDtotal_amount" value="<?php echo $order_info['total_fee'];?>" />
                                    <!--<input type="hidden" name="WIDshow_url" value="http://<?php echo WWW_DOMAIN;?>/member/order/orderinfo.php?id=<?php echo $order_info['order_sn'];?>" />-->      
                                    </div>
                                    </form>
                                </div>
                                
                            </div>

                            <!-- 线下支付-start -->
                            <div class="payment_user_info box">
                                <p>收款户名 ：<span>上海义文机电有限公司</span></p>
                                <p>收款账号 ：<span>3100 6950 3013 0050 23622</span><button id="card_copy">复制</button></p>
                                <p>开户银行 ：<span>交通银行上海临港支行</span></p>
                                <div class="upload_credentials box" style="position: relative;">
                                    <h3>+</h3>
                                    <p>上传付款凭证</p>
                                    <form action="./order.php" id="uploadForm" method="post" enctype="multipart/form-data">
                                        <input type="hidden" name="id" value="<?php echo $order_info["order_sn"]; ?>">
                                        <input type="hidden" name="order_id" value="<?php echo $order_info['order_id']; ?>">
                                        <input type="hidden" name="act" value="finishPay">
                                        <input type="file" name="file" style="opacity: 0;border: 1px solid red;width: 150px;height: 120px;position: absolute;top: 0;">
                                        <img style="display: inline-block;width: 150px;height: 120px;position: absolute;top: 0;" class="showImg" src="" alt="">
                                    </form>
                                </div>
                            </div>
                            <div class="zhanwei" style="height: 27px;widht: 100%;display: none;"></div>
                            <div class="payment_status" onclick="hasPay()">已付款</div>
                        </div>
                    </div>
                    <?php endif;?>
                </div>
            </div>

            <!-- 支付信息弹框 -->
            <div class="payment_alert_block">
                <div class="payment_alert_content transform box">
                    <div id="pay_title" class="tit">支付成功！请耐心等待发货</div>
                    <!-- 线下支付 -->
                    <div class="tit" style="display: none">订单提交成功，工作人员会及时与您联系!</div>
                    <div class="number">订单号：<span class="e9"><?php echo $order_info['order_sn']; ?></span></div>
                    <p>支付方式：<span id="payType"></span></p>
                    <p>支付金额：<span class="e9">￥<?php echo $order_info['total_fee'];?></span></p>
                    <div class="btns">
                        <a onclick="goOrder()" class="fl">查看订单</a>
                        <a href="./index.php" class="fr">商城首页</a>
                    </div>
                </div>
            </div>

            
<?php include('./footer.php');?>
<script type="text/javascript" src="/js/artDialog4.1.7/artDialog.js?skin=blue"></script> 

<script>
    function goOrder(){
        location.href = "/newmember/booking.php"
    }
    $('#card_copy').click(function() {
        var text = "3100 6950 3013 0050 23622";
        var input = $('<input>').val(text).appendTo('body').select();
        document.execCommand('copy');
        input.remove();
        alert('复制成功');
      });

    $(".showImg").hide()
    function zhifubaoPay(){
        $("#zfb_pay").attr("checked", true)
        $(".payment_user_info").hide()
        $(".zhanwei").show();
        $(".payment_alert_block").show()
        $("#payType").text("支付宝")
        $("#pay_title").text("请在支付宝支付后查看详情")
    }
    function xianxiaPay(){
        $("#xianxia_pay").attr("checked", true)
        $(".payment_user_info").show()
        $(".zhanwei").hide();
        $("#payType").text("线下支付")
    }
    function hasPay(){
        if($(".showImg").attr('src') == ""){
            alert("请上传付款凭证")
            return
        }
        var form = document.getElementById("uploadForm");
        $(".payment_alert_block").show()
        setTimeout(() => {
            form.submit();
        }, 1000);
    }
    $("input[type='file']").change(function(){
         $filePath=URL.createObjectURL(this.files[0]);
         //这是获取到图片元素，因为我这里的页面结构是统一的，input表单的上一个元素是img，
         //所以这个地方只是为了获取你的img元素，然后设置src属性即可
        $(".showImg").show()
        $(".showImg").attr('src',$filePath);
    });
    function cancleOrder(orderId){
        art.dialog({
				content: '您确定要取消订单吗？',
				//time: 3,
				title: '系统提示',
				opacity: 0.2,	// 透明度
				icon: 'succeed',
				lock: true,
                fixed: true,
                top: "200px",
				button: [
					{
						name: '取消',
						callback: function () {
							window.location.reload()
							return false;
						},
						focus: false
					},
					{
						name: '确定',
						callback: function () {
                            $.get("./order.php", {
                                act: "cancleOrder",
                                order_id: orderId,
                                id: "<?php echo $order_info["order_sn"] ?>"
                            }, function(data){
                                var res = JSON.parse(data)
                                if (res.type == 1){
                                    window.location.reload()
                                }
                            })
							return false;
							//alert('你不同意')
						},
						focus: true
					}
				]
			});
    }

</script>